#ifndef A5F33391_9E9E_4431_923B_ACA547C7C9F6
#define A5F33391_9E9E_4431_923B_ACA547C7C9F6

#include <poll.h>
#include <sys/types.h>
#include <sys/time.h>
#include <iostream>
#include "Sock.hpp"

static const uint16_t defaultport = 8080;
static const int fd_max_num = 1024;
static const int defaultfd = -1;
const int non = 0;

class pollserver
{
public:
	pollserver(uint16_t port = defaultport) : _port(port)
	{
		for (int i = 0; i < fd_max_num; i++)
		{
			event_fds[i].fd = defaultfd;
			event_fds[i].events = non;
			event_fds[i].revents = non;
		}
	}
	void Init()
	{
		_listensock.Socket();
		_listensock.Bind(_port);
		_listensock.Listen();
	}
	void accepter()
	{
		std::string clientip;
		uint16_t port;
		int newfd = _listensock.Accept(&clientip, &port);
		if (newfd < 0)
			return;
		else
		{
			// printf("Get a new link\n");
			int pos = 0;
			for (; pos < fd_max_num; pos++)
			{
				if (event_fds[pos].fd == defaultfd)
					break;
			}
			if (pos == fd_max_num)
			{
				lg(Warnning, "fd_array is full,can`t accept");
				close(newfd);
			}
			else
			{
				event_fds[pos].fd = newfd;
				event_fds[pos].events = POLLIN;
				event_fds[pos].revents = non;
				lg(Info, "Get a new link,clientip : %s,clientfd: %d", clientip.c_str(), newfd);
				Printfd();
			}
		}
	}
	void recver(int fd,int i)
	{
		char buffer[1024];
		int n = read(fd, buffer, sizeof(buffer) - 1);   //// 一定存在bug，不能保证读取到的是完整的报文！！！
		if (n == 0)
		{
			lg(Info, "client shut down,fd : %d close", fd);
			close(fd);
			event_fds[i].fd = defaultfd; /// 将这个fd移除 --- select不再需要关心
			event_fds[i].events = non;
		}
		else if (n < 0)
		{
			lg(Error, "read error,errno:%d,errstring:%s", errno, strerror(errno));
			close(fd);
			event_fds[i].fd = defaultfd;
			event_fds[i].events = non;
		}
		else
		{
			buffer[n] = 0;
			std::cout << "Get a messgae : " << buffer << std::endl;
		}
	}
	void Dispatcher()
	{
		for (int i = 0; i < fd_max_num; i++)
		{
			int fd = event_fds[i].fd;
			if (event_fds[i].revents & POLLIN)
			{
				if (fd == _listensock.Fd())
				{
					// sockaddr_in client;
					// socklen_t len = sizeof(client);
					accepter();
				}
				else
				{
					recver(fd,i);
				}
			}
		}
	}
	void Start()
	{
		int listenfd = _listensock.Fd();
		int maxfd = 0;
		event_fds[0].fd = listenfd;
		event_fds[0].events = POLLIN;
		for (;;)
		{
			// for (int i = 0; i < fd_max_num; i++)
			// {
			// 	if (event_fds[i].fd == defaultfd)
			// 		continue;
			// 	event_fds[i].events = POLLIN;
			// 	event_fds[i].revents = non;
			// 	if (event_fds[i].fd > maxfd)
			// 	{
			// 		maxfd = event_fds[i].fd;
			// 	}
			// }
			int n = poll(event_fds,fd_max_num,1000); // 0为非阻塞轮询，-1为阻塞
			switch (n)
			{
			case 0:
				std::cout << "time out" << std::endl;
				break;
			case -1:
				lg(Error, "select error , errno : %d,err str: %s", errno, strerror(errno));
				break;
			default:
				Dispatcher();
				break;
			}
		}
	}
	void Printfd()
	{
		printf("online lists : ");
		for(int i = 0;i < fd_max_num;i++){
			if(event_fds[i].fd == defaultfd) continue;
			std::cout << event_fds[i].fd << " ";   ///////  -------- 不要忘记了缓冲区刷新问题
		}
		std::cout << std::endl;
	}
	~pollserver()
	{
		_listensock.Close();
	}

private:
	Sock _listensock;
	uint16_t _port;
	// int fd_array[fd_max_num];
	struct pollfd event_fds[fd_max_num];
};

#endif /* A5F33391_9E9E_4431_923B_ACA547C7C9F6 */
